using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using Bizspoke.BizTalk.Adapter;
using Bizspoke.BizTalk.Brma;
using Bizspoke.BizTalk.HATPlugIn.BizTalk.Extension.HATPlugIn;
using Microsoft.BizTalk.Tracking;

namespace Bizspoke.BizTalk.HATPlugIn
{
    [Microsoft.BizTalk.TrackingOld.HatContextMenuPluginAttribute]
    public class ContextMenu2 : Microsoft.BizTalk.TrackingOld.ICtxMenuPlugin, IOverrideDbLocation, INavigateNewWindowRequesting
    {
        //private static string _messageInstanceID = string.Empty;
        //private static Hashtable _ServiceTypeIds = null;
        private static readonly string name = "(Bizspoke) View tracked messages...";

        #region INavigateNewWindowRequesting Members

        public event NavigateNewWindowRequestEvent NavigateNewWindowRequest;

        #endregion

        /*private Hashtable ServiceTypeIds
        {
            get
            {
                if (_ServiceTypeIds == null)
                {
                    _ServiceTypeIds = this.GetServiceTypeIds();
                }
                return _ServiceTypeIds;
            }
        }*/

        private bool IsAdministrator
        {
            get
            {
                bool isAdmin = UserRole.IsBizTalkServerAdministrator();
                if (!isAdmin)
                    MessageBox.Show("You need to be an administrator", "Permission Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return isAdmin;
            }
        }

        #region ICtxMenuPlugin Members

        public string UiName
        {
            get { return name; }
        }

        public void OnClick(string viewName, ICollection items)
        {
            if (this.NavigateNewWindowRequest != null && items != null && items.Count > 0)
            {
                ProgressForm progressForm = new ProgressForm(viewName, items);
                if (progressForm.ShowDialog() != DialogResult.OK)
                    return;

                if (progressForm.TrackedInstances != null & progressForm.TrackedInstances.Count > 0 && !string.IsNullOrEmpty(progressForm.OutputFolder))
                {
                    MessageView msgview = new MessageView(progressForm.TrackedInstances, progressForm.OutputFolder);
                    msgview.ShowDialog();
                }
            }
        }

        public SelectionSupport PreprocessSelection(string viewName, ICollection items)
        {
            return SelectionSupport.SupportedByPlugin;

            if (UserRole.IsBizTalkServerAdministrator() && (items.Count > 0))
            {
                return SelectionSupport.SupportedByPlugin;
                /*
                foreach (TrackingDataItem item in items)
                {
                    foreach (TrackingProperty property in item.TrackingProperties)
                    {
                        if ("OperationsMsgs.htm" == viewName)
                        {
                            if ((Info.AdapterName == Convert.ToString(property.Value)) && ((TrackingPropertyId.InboundAdapter == property.Id) || (TrackingPropertyId.OutboundAdapter == property.Id)))
                            {
                                return SelectionSupport.SupportedByPlugin;
                            }
                            if ((TrackingPropertyId.ServiceTypeId == property.Id) && this.IsAdapterServiceType(new Guid(Convert.ToString(property.Value))))
                            {
                                return SelectionSupport.SupportedByPlugin;
                            }
                            continue;
                        }
                        if ("FindMsg.htm" == viewName)
                        {
                            if ((Info.AdapterName == Convert.ToString(property.Value)) && (TrackingPropertyId.Adapter == property.Id))
                            {
                                return SelectionSupport.SupportedByPlugin;
                            }
                            continue;
                        }
                        return SelectionSupport.NotSupportedByPlugin;
                    }
                }
                */
            }
            return SelectionSupport.NotSupportedByPlugin;
        }

        /*private bool IsAdapterServiceType(Guid serviceTypeId)
        {
            return this.ServiceTypeIds.ContainsKey(serviceTypeId);
        }*/

        /*private Hashtable GetServiceTypeIds()
        {
            Hashtable hashtable = new Hashtable();
            string format = "SELECT DISTINCT bts_sendport.uidGUID FROM bts_sendport, bts_sendport_transport, adm_Adapter WHERE bts_sendport.nID = bts_sendport_transport.nSendPortID AND bts_sendport_transport.nTransportTypeId = adm_Adapter.Id AND adm_Adapter.MgmtCLSID = '{0}'";
            DataTable table = SqlHelper.ExecuteQuery(string.Format(format, Info.ConfigurationGuid.ToString()), new BtsMgmtDbInfo());
            if ((table != null) && (table.Rows.Count > 0))
            {
                foreach (DataRow row in table.Rows)
                {
                    hashtable.Add(row[0], row[0]);
                }
            }
            return hashtable;
        }*/

        #endregion

        #region IOverrideDbLocation Members

        public void SetMgmtDB(string mgmtDbName, string mgmtDbServerName)
        {
            Info.BtsMgmtDBName = mgmtDbName;
            Info.BtsMgmtDBServer = mgmtDbServerName;
        }

        public void SetTrackingDB(string archiveDbName, string archiveDbServerName)
        {
            Info.BtsTrackingDBName = archiveDbName;
            Info.BtsTrackingDBServer = archiveDbServerName;
        }

        #endregion

        #region ICtxMenuPlugin Members

        Microsoft.BizTalk.TrackingOld.SelectionSupport Microsoft.BizTalk.TrackingOld.ICtxMenuPlugin.PreprocessSelection(string viewName, ICollection items)
        {
            return Microsoft.BizTalk.TrackingOld.SelectionSupport.SupportedByPlugin;
        }

        #endregion
    }
}
